import { areaList, townList } from 'common/js/modules'

const areaData = areaList()
const townData = townList()

const mutations = {
	SET_DATA (state, arr) {
		let arr1 = [
			{ tjName: '水情正常' },
			{ tjName: '水情延迟' },
			{ tjName: '水情缺失' },
			{ tjName: '工情正常' },
			{ tjName: '工情缺失' },
			{ tjName: '视频全部在线' },
			{ tjName: '视频部分在线' },
			{ tjName: '视频全部离线' },
			{ tjName: '视频视频缺失' }
		]

		const obj = {
          '禅城区': {},
          '顺德区': {},
          '南海区': {},
          '高明区': {},
          '三水区': {},
          '佛山市': {},
        }

		arr1.map(item => {
			for (let key in obj) {
				obj[key][item.tjName] = 0

				if (key !== '佛山市') {
					const objarea = areaData.find(child => child.label === key)
	
					if (obj[key]['town'] === undefined) {
						obj[key]['town'] = {}
					}
					
					const towns = townData.filter(child => child.value.indexOf(objarea.value) > -1)
					towns.map(child => {
						if (obj[key]['town'][child.label] === undefined) {
							obj[key]['town'][child.label] = {}
						}
						obj[key]['town'][child.label][item.tjName] = 0
					})
					if (obj[key]['town']['东风水库'] === undefined) {
						obj[key]['town']['东风水库'] = {}
					}
					obj[key]['town']['东风水库'][item.tjName] = 0

					if (obj[key]['town']['未知镇'] === undefined) {
						obj[key]['town']['未知镇'] = {}
					}
					obj[key]['town']['未知镇'][item.tjName] = 0
				}
			}
		})
        arr.map(item => { 
			obj[item.addvcdName][`水情${item.pumpStatus}`] += 1
        	obj[item.addvcdName][`工情${item.__custom_detailStatus}`] += 1
        	obj[item.addvcdName][`视频${item.__custom_videoDetailStatus}`] += 1
			
			
			if (item.addvcdName !== '佛山市' && obj[item.addvcdName]['town']) {
				if (item.zhenname) {
					obj[item.addvcdName]['town'][item.zhenname][`水情${item.pumpStatus}`] += 1
					obj[item.addvcdName]['town'][item.zhenname][`工情${item.__custom_detailStatus}`] += 1
					obj[item.addvcdName]['town'][item.zhenname][`视频${item.__custom_videoDetailStatus}`] += 1
				} else {
					obj[item.addvcdName]['town']['未知镇'][`水情${item.pumpStatus}`] += 1
					obj[item.addvcdName]['town']['未知镇'][`工情${item.__custom_detailStatus}`] += 1
					obj[item.addvcdName]['town']['未知镇'][`视频${item.__custom_videoDetailStatus}`] += 1
				}
			}
        })

		for (let key in obj) {
			for (let key2 in obj[key]) {
				const obj1 = arr1.find(item => item.tjName === key2) || {}
				obj1[key] = obj[key][key2] || 0
			}

			if (obj[key]['town']) {
				obj[key]['townArr'] = [
					{ tjName: '水情正常' },
					{ tjName: '水情延迟' },
					{ tjName: '水情缺失' },
					{ tjName: '工情正常' },
					{ tjName: '工情缺失' },
					{ tjName: '视频全部在线' },
					{ tjName: '视频部分在线' },
					{ tjName: '视频全部离线' },
					{ tjName: '视频视频缺失' }
				]
				// console.log(obj[key]['town'])
				for (let key2 in obj[key]['town']) {
					for (let key3 in obj[key]['town'][key2]) {
						const obj1 = obj[key]['townArr'].find(item => item.tjName === key3) || {}
						obj1[key2] = obj[key]['town'][key2][key3] || 0
					}
				}
			}
		}

		state.total = arr1
		state.totalObj = obj
	}
}

export default mutations
